package cn.jekun.shop.web.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

import cn.jekun.component.base.bean.ErrorMsg;
import cn.jekun.component.base.exceptions.BusinessException;
import cn.jekun.component.core.Constants;
import cn.jekun.component.core.bean.gen.ErpCartAttr;
import cn.jekun.component.shop.bean.Goods;
import cn.jekun.component.shop.service.CommonsCartService;
import cn.jekun.shop.common.service.CartService;
import cn.jekun.shop.common.service.ShopBaseAction;


/**
 * 购物车
 * @author Mr.Dong
 *
 */
@SuppressWarnings("serial")
public class CartAction extends ShopBaseAction{
	@Resource(name = CartService.BEAN_NAME)
	protected CartService cartService;
    
	@Resource(name=CommonsCartService.BEAN_NAME)
	CommonsCartService commonsCartService;//购物车
	
	private List<Goods> goods  = new ArrayList<Goods>();
	
	private List<ErpCartAttr> attrs = new ArrayList<ErpCartAttr>();

	public List<Goods> getGoods() {
		return goods;
	}

	public void setGoods(List<Goods> goods) {
		this.goods = goods;
	}

	public List<ErpCartAttr> getAttrs() {
		return attrs;
	}

	public void setAttrs(List<ErpCartAttr> attrs) {
		this.attrs = attrs;
	}

	/**
	 * 商品加入购物车
	 * @return
	 */
	public String addshoptoCart(){
		JSONObject jsonObject = new JSONObject();
		try {
			//验证是否登录
			String sessionId = null;
			if(!isLogin()){
				//sessionId = this.request.getSession().getId();
				return timeout();
			}
			String type = request.getParameter("type");
			String merchandiseId=request.getParameter("merchandiseId");
			if(StringUtils.isBlank(request.getParameter("type"))){
				type = Constants.CART_TYPE_ADD;
			}
			//jsonObject = this.commonsCartService.addShopToCart2(getUserId(), sessionId, type, goods, attrs);
            
			jsonObject = this.commonsCartService.addMerchandiseToCart(getUserId(), sessionId, type, merchandiseId, null);

			
			
			this.logService.addMallLog(this.getUserId(), 
					this.getBrowser(), this.getIp(), "商品加入购物车成功，来源:web");//记录操作日志

		} catch(BusinessException e){
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		
			
			this.logService.addMallLog(this.getUserId(), 
					this.getBrowser(), this.getIp(), "商品加入购物车失败，原因:"+jsonObject.get("message")+",来源:web");//记录操作日志

		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);	
	}
//	/**
//	 * 旧版商城加入购物车
//	 */
//	public String oldAddShopToCart(){
//		JSONObject jsonObject = new JSONObject();
//		try {
//			//验证是否登录
//			String sessionId = null;
//			if(!isLogin()){
//				sessionId = this.request.getSession().getId();
//			}
//			String type = request.getParameter("type");
//			if(StringUtils.isBlank(request.getParameter("type"))){
//				type = Constants.CART_TYPE_ADD;
//			}
//			jsonObject = this.commonsCartService.addShopToCart2(getUserId(), sessionId, type, goods, attrs);
//            
////			jsonObject = this.commonsCartService.addMerchandiseToCart(getUserId(), sessionId, type, merchandiseId, null);
//
//			
//			
//			this.logService.addMallLog(this.getUserId(), 
//					this.getBrowser(), this.getIp(), "商品加入购物车成功，来源:web");//记录操作日志
//
//		} catch(BusinessException e){
//			jsonObject = JSONObject.fromObject(e.getErrorMsg());
//		
//			
//			this.logService.addMallLog(this.getUserId(), 
//					this.getBrowser(), this.getIp(), "商品加入购物车失败，原因:"+jsonObject.get("message")+",来源:web");//记录操作日志
//
//		} catch (Exception e) {
//			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
//			e.printStackTrace();
//		}
//		return this.writeJson(jsonObject);	
//	}
	/**
	 * 获取购物车商品数量
	 * @return
	 */
	public String getCartShopNum(){

		JSONObject jsonObject = new JSONObject();
		String userId =null,sessionId = null,shopId=null;
		//验证是否登录
		if(!isLogin()){
			sessionId = this.request.getSession().getId();
		}else{
			userId = this.getUserId();
		}
		shopId=this.request.getParameter("shopId");
		jsonObject = cartService.getCartShopNums(userId,sessionId,shopId);
		return this.writeJson(jsonObject);
	}

	/**
	 * 获取购物车商品信息(列表)
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String getCartShops(){
		JSONObject jsonObject = new JSONObject();
		//验证是否登录
		if(!isLogin()){
			formMap.put("sessionId", this.request.getSession().getId());
		}else{
			formMap.put("userId", this.getUserId());
		}
		
		formMap.put("shopId", this.request.getParameter("shopId"));
		
		jsonObject = cartService.getCartShops(formMap);
		return this.writeJson(jsonObject);
	}

	/**
	 * 修改购物车商品数量
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String updateCartShopsNum(){
		JSONObject jsonObject = new JSONObject();
		try {
			formMap.put("cartId", request.getParameter("cartId"));
			formMap.put("nums", request.getParameter("nums"));
			jsonObject = cartService.updateCartshopNum(formMap);
			
			this.logService.addMallLog(this.getUserId(), 
					this.getBrowser(), this.getIp(), "修改购物车商品数量成功,来源:web,购物车代码:"+request.getParameter("cartId"));//记录操作日志

		}catch(BusinessException e){
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
			this.logService.addMallLog(this.getUserId(), 
					this.getBrowser(), this.getIp(), "修改购物车商品数量失败,原因:"+jsonObject.get("message")+",来源:web,购物车代码:"+request.getParameter("cartId"));//记录操作日志

		}
		catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}

	/**
	 * 删除购物车商品
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String deleteCartShop(){
		JSONObject jsonObject = new JSONObject();
		try {
			
			formMap.put("cartId", request.getParameter("cartId"));
			jsonObject = cartService.deleteCartShops(formMap);
			
			this.logService.addMallLog(this.getUserId(), 
					this.getBrowser(), this.getIp(), "删除购物车商品成功,来源:web,购物车代码:"+request.getParameter("cartId"));//记录操作日志

		}catch(BusinessException e){
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
			
			this.logService.addMallLog(this.getUserId(), 
					this.getBrowser(), this.getIp(), "删除购物车商品失败,原因:"+jsonObject.get("message")+",来源:web,购物车代码:"+request.getParameter("cartId"));//记录操作日志

		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}


}
